package com.teachoa.web;

import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.teachoa.bean.Ask;
import com.teachoa.bean.Course;
import com.teachoa.bean.Msg;
import com.teachoa.bean.Project;
import com.teachoa.bean.Uncertain;
import com.teachoa.bean.UserTask;
import com.teachoa.enumtype.MenuEnum;
import com.teachoa.enumtype.UserTypeEnum;
import com.teachoa.util.L;
import com.teachoa.util.Lists;

@SuppressWarnings({"rawtypes","unchecked"})
public class MainController extends BaseController {
	
	public void index() {
		setAttr("msgs", Msg.dao.listTopSysMsgs());
//		org.snaker.engine.access.Page<WorkItem> majorPage = new 
//				org.snaker.engine.access.Page<WorkItem>(5);
//		List<WorkItem> works = super.getSnakerEngine()
//				.query()
//				.getWorkItems(majorPage, new QueryFilter()
//				.setOperators(new String[]{super.getCurrentUserId()})
//				.setTaskType(TaskType.Major.ordinal()));
//		//System.out.println(works);
//		for(WorkItem wi:works) {
//			String orderId = wi.getOrderId();
//			Project project = Project.dao.findProjectByOrderId(orderId);
//			wi.setProcessName(project.getStr("name"));
//		}
		String userId = super.getCurrentUserId();
		List<Record> works = UserTask.dao.findWaitTasksByUserId(userId);
		setAttr("works", works);
		
		int userType = super.getCurrentUserType();
		System.err.println(userId);
		
		Page<Course> coursePage = Course.dao.findByPage(1);
		setAttr("coursePage", coursePage);
		
		Page<Ask> askPage = Ask.dao.findAsksByPage(1, userType, userId);
		setAttr("askPage", askPage);
		
		Page<Msg> msgPage = Msg.dao.findMsgsByPage(1, userId);
		setAttr("msgPage", msgPage);
		
		Page<Uncertain> uncertainPage = null;
		if(userType==UserTypeEnum.STUDENT.getCode())
			uncertainPage = Uncertain.dao.getUncertainPageByStudentId(1, userId);
		else if(userType==UserTypeEnum.TEACHER.getCode())
			uncertainPage = Uncertain.dao.getUncertainPageByTeacherId(1, userId);
		setAttr("uncertainPage", uncertainPage);
		
		Page<Project> projectPage = Project.dao.findProjectsByPage(1, userType, userId);
		setAttr("projectPage", projectPage);
		
//		List<Process> list = getSnakerEngine().process().getProcesss(new QueryFilter());
//		setAttr("flows", list);
		
		setAttr("userType", UserTypeEnum.getEnumByCode(userType).name().toLowerCase());
		renderJsp("main.jsp");
	}
	
	public void content() {
		setAttr("msg", Msg.dao.findById(getPara()));
		renderJsp("content.jsp");
	}
	
	public void side() {
		Map user = getSessionAttr("currentUser");
		int type = L.toInt(user.get("type"));
		String userId = L.toString(user.get("id"));
		String menuStr = UserTypeEnum.getEnumByCode(type).getMenus();
		String[] menuCodes = menuStr.split(",");
		List menuList = Lists.newArrayList();
		for(String code:menuCodes) {
			menuList.add(MenuEnum.getEnumByCode(Integer.parseInt(code)));
		}
		setAttr("menuList", menuList);
		setAttr("msgs", Msg.dao.listOwnMsgs(userId));
		renderJsp("side.jsp");
	}

}
